<?php
namespace Addons\WechatOpenPlatform\Controller\Admin;
use Lib\Response;
use Session;
use Models\BaseModel as Model;
use Addons\QyWechat\Helper;

use Addons\WechatOpenPlatform\Services\Wxopenapi;
require_once(DOXCX_FRAME_PATH . 'fun/library/QyWxCypto/WXBizMsgCrypt.php'); 

class ReceiveTicke extends \BaseController
{
	public function index(){
		$timestamp = $_GET['timestamp'];
		$nonce = $_GET['nonce'];
		$msg_signature = $_GET['msg_signature'];
		//获取xml冰去除特殊字符
		$xml = preg_replace("/[\n\s]/", '', file_get_contents('php://input'));
		//解密
		$admin=(new Model('wx_open_confing'))->find(['AppId'=>xml_to_array($xml)['AppId']]);
		$wx = new \WXBizMsgCrypt($admin->token, $admin->EncodingAESKey, $admin->AppId);
		$msg = '';
		$err_code = $wx->DecryptMsg($msg_signature, $timestamp, $nonce, $xml, $msg); 
		if ($err_code == 0) {
			$arr = xml_to_array($msg);
			$cvt = (new Model('component_verify_ticket'))->find(['app_id'=>$arr['AppId']]);
			if (!$cvt->id) {
			   $cvt->app_id = $arr['AppId'];
			}
			$cvt->info_type = $arr['InfoType'];
			$cvt->component_verify_ticket = $arr['ComponentVerifyTicket'];
			$cvt->text = $msg;
			$cvt->save();
		    //print("解密后: " . $msg . "\n");
		} else {
		    print($err_code . "\n");
		}
		echo "success";
		exit();

	}

	//授权管理 
	public function platform(){
		$wid = \Session::get("wid");
		$list = (new Model("qy_gzh_token"))->where(["wid"=>$wid])->list_all();
		foreach ($list as $key => $value) {
			$list[$key]->message = json_decode($value->message);
		}
		$this->assign("list", $list);
		return $this->show();
	}

	//升起二维码扫码进入
	public function login(){
		$QyWechatApi = Wxopenapi::rebind();
	}

	//扫码之后返回地址获取令牌
	public function call_back(){
		$auth_code = $_GET['auth_code'];
		$wid = $_GET['wid'];
		$authorizer_appid = Wxopenapi::get_authorizer_access_token($auth_code,$wid);
		if($authorizer_appid){
			$fissions = (new Model("qy_fission"))->where(["wid"=>$wid,"status"=>2,"authorizer_appid"=>$authorizer_appid])->list_all();
			foreach ($fissions as $key => $value) {
				$value->status = 1;
				$value->save();
			}
		}
		\Redirect::to('platform'); 
	}
 

	public function del(){
		setApi();
		$wid = \Session::get("wid");
		$id = $_POST["id"];
		$list = (new Model("qy_gzh_token"))->find($id);
		$fissions = (new Model("qy_fission"))->where(["wid"=>$wid,"end_time@>="=>time(),"status"=>1,"authorizer_appid"=>$list->authorizer_appid])->list_all();
		foreach ($fissions as $key => $value) {
			$value->status = 2;
			$value->save();
		}
		if($list->delete()){
			sendSuccess("操作成功");
		}
		sendError("操作失败");
	}

	public function set_config(){
		setApi();
		$wid = Session::get('wid');
	  	$gzh_list = (new Model('qy_gzh_token'))->where(['wid' => $wid])->list_all_array();
	  	foreach ($gzh_list as $key => $value) {
	  		$gzh_list[$key]["message"] = json_decode($value["message"])->authorizer_info->nick_name;
	  	}
		$this->assign("gzh_list", $gzh_list);
	  	return $this->show();
	}

	public function upd_set(){
		setApi();
		$id = $_POST["id"];
		$wid = Session::get('wid');
	  	$gzh_list = (new Model('qy_gzh_token'))->where(['wid' => $wid])->list_all();
	  	foreach ($gzh_list as $key => $value) {
	  		if($value->authorizer_appid == $id){
	  			$value->is_checked = 1;
	  		}else{
	  			$value->is_checked = 0;
	  		}
	  		$value->save();
	  	}
	  	sendSuccess("修改成功");
	}
}
?>